package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;


    @Override
    public ResponseResult saveSensitive(SensitiveDto dto) {

        if (dto == null) {
            log.warn("传入敏感词不能为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        String name = dto.getName();
        if (StringUtils.isBlank(name)) {
            log.warn("传入敏感词不能为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        WmSensitive wmSensitive = new WmSensitive();
        wmSensitive.setSensitives(name);
        wmSensitive.setCreatedTime(new Date());

        int insertResult = wmSensitiveMapper.insert(wmSensitive);
        if (insertResult < 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"数据更新失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public PageResponseResult listSensitive(SensitiveDto dto) {

        if (dto == null) {
            log.warn("listSensitive传入参数为空");
            return new PageResponseResult(0,0,0);
        }

        String name = dto.getName();
        if (StringUtils.isBlank(name)) {
            log.warn("name为空");
            return new PageResponseResult(0,0,0);
        }

        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getSensitives,name)
                .orderByDesc();

        dto.checkParam();
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        Page<WmSensitive> wmSensitivePage = new Page<>(page,size);

        Page<WmSensitive> pageResult = wmSensitiveMapper.selectPage(wmSensitivePage, wrapper);
        Long total = pageResult.getTotal();

        PageResponseResult pageResponseResult = new PageResponseResult(page,size,total.intValue());
        pageResponseResult.setData(pageResult.getRecords());


        //todo 这里可能要用PageResponseResult作返回值
        return pageResponseResult;
    }

    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {

        if (wmSensitive == null || wmSensitive.getId() == null || StringUtils.isBlank(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        this.updateById(wmSensitive);

//        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(WmSensitive::getSensitives,sensitives);
//        wmSensitiveMapper.updateById(wmSensitive);


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteSensitive(Integer id) {

        if (id == null) {
            log.warn("传入id为空,无法删除");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        this.removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
